-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor!: upgrade thiserror
and refine error handling
#88
Conversation
Upgraded `thiserror` version and made specific error messages clearer. Adjusted error variants and assertions to provide more meaningful feedback, enhancing clarity in debug and runtime scenarios.
WalkthroughThe pull request includes modifications to the Changes
Possibly related PRs
Suggested reviewers
Poem
Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media? 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. CodeRabbit Commands (Invoked using PR comments)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Outside diff range and nitpick comments (8)
src/utils/sqrt.rs (1)
Line range hint
22-49
: Add test coverage for negative input case.While the test coverage is comprehensive for valid inputs, there's no test verifying the error handling for negative inputs. Consider adding a test like this:
#[test] fn test_sqrt_negative() { let negative = BigInt::from(-1); match sqrt(&negative) { Err(Error::Invalid(msg)) => assert_eq!(msg, "NEGATIVE"), _ => panic!("Expected Error::Invalid(\"NEGATIVE\") for negative input"), } }src/error.rs (3)
12-14
: Consider enhancing the documentation with an example.The rename to
UintOverflow
and the specific mention ofU256::MAX
in the error message are good improvements.Consider adding an example to the documentation to show when this error might occur:
-/// Triggers when it tries to exceed [`alloy_primitives::U256::MAX`]. +/// Triggers when a numeric operation would exceed [`alloy_primitives::U256::MAX`]. +/// +/// # Example +/// This error occurs in operations like currency amount multiplication where +/// the result would exceed the maximum allowed value.
20-22
: Consider adding validation and documentation for Invalid variant.While the addition of a
&'static str
parameter allows for more specific error messages, consider:
- Adding documentation about expected format/content of error messages
- Creating constants for common error messages to ensure consistency
/// Common error messages for the Invalid variant pub mod error_messages { pub const SIGNIFICANT_DIGITS: &str = "SIGNIFICANT_DIGITS"; // Add other common error messages here }
43-55
: Consider adding negative test cases.While the current tests verify the error messages correctly, consider adding tests for:
- Invalid error messages (empty strings, very long messages)
- Different chain ID combinations
- Edge cases for currency mismatches
#[test] fn test_chain_id_mismatch_combinations() { let cases = [(0, 1), (u64::MAX, 0), (u64::MAX, u64::MAX - 1)]; for (id1, id2) in cases { let error = Error::ChainIdMismatch(id1, id2); assert_eq!(error.to_string(), format!("chain IDs do not match: {} and {}", id1, id2)); } }src/utils/sorted_insert.rs (3)
11-16
: Consider the implications of switching from Results to panics.The change from
Result<Option<T>, Error>
toOption<T>
with assertions represents a significant shift in error handling strategy. While this simplifies the API, it also means that invalid inputs will now cause panics instead of returnable errors.This is a breaking change that:
- Makes the function more ergonomic for valid inputs
- Treats
max_size
validation as a programming error rather than a runtime error- May require updates to error handling in consuming code
Consider whether these conditions truly represent programming errors (that should panic) rather than runtime errors (that should return
Result
). If these could be runtime conditions, consider keeping theResult
return type for more graceful error handling.
56-59
: Enhance the array length validation test.While the test correctly verifies the panic message, it could be more thorough by testing with different array and max sizes.
Consider adding more test cases:
#[test] #[should_panic(expected = "array length cannot exceed max_size")] fn test_length_greater_than_max_size_variants() { // Test with different array sizes exceeding max_size let mut arr1 = vec![1, 2, 3]; sorted_insert(&mut arr1, 1, 2, cmp); let mut arr2 = vec![1, 2, 3, 4, 5]; sorted_insert(&mut arr2, 1, 3, cmp); }
Line range hint
3-5
: Update documentation to reflect panic conditions.The function documentation should be updated to explicitly mention the panic conditions for invalid inputs.
Consider updating the documentation:
/// Given an array of items sorted by `comparator`, insert an item into its sort index and constrain /// the size to `maxSize` by removing the last item +/// +/// # Panics +/// +/// This function will panic if: +/// - `max_size` is zero +/// - The length of `items` exceeds `max_size`src/entities/fractions/currency_amount.rs (1)
126-126
: Consider enhancing the error message with more context.While the change from
NotEqual
toInvalid("DECIMALS")
is an improvement, the error message could be more specific about why the decimals are invalid.Consider using a more descriptive error message that includes the actual and maximum allowed decimal places:
-return Err(Error::Invalid("DECIMALS")); +return Err(Error::Invalid(&format!( + "Requested {decimal_places} decimals exceeds currency's {max} decimals", + max = self.currency.decimals() +)));
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
📒 Files selected for processing (7)
Cargo.toml
(2 hunks)src/entities/fractions/currency_amount.rs
(4 hunks)src/entities/fractions/fraction.rs
(1 hunks)src/entities/fractions/price.rs
(2 hunks)src/error.rs
(1 hunks)src/utils/sorted_insert.rs
(3 hunks)src/utils/sqrt.rs
(1 hunks)
🔇 Additional comments (14)
Cargo.toml (3)
3-3
: LGTM: Version bump follows semantic versioning.
The version increment from 3.0.0 to 3.1.0 appropriately reflects the thiserror upgrade and error handling improvements.
23-23
: LGTM: Feature declaration correctly updated.
The std feature now properly includes thiserror/std, which is the correct way to enable std features when default-features is disabled.
20-20
: Verify the impact of thiserror v2 upgrade.
The upgrade from thiserror 1.0 to 2.0 is a major version change that could introduce breaking changes. The addition of default-features = false
is good for no_std compatibility.
Let's verify the changes:
✅ Verification successful
Let me check the specific error implementations to better understand the impact.
The upgrade to thiserror v2 is safe for this codebase
The error implementation in this codebase is straightforward and only uses basic thiserror features that are stable across versions:
- Simple enum with static error messages
- Basic parameter interpolation with numbered placeholders
- Standard derive macros alongside thiserror
The usage patterns are fully compatible with thiserror v2, and the default-features = false
flag is appropriate for the no_std support shown in the test configuration.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Check for potential breaking changes in error handling
# Test 1: Look for thiserror derive macro usage
rg -l '#\[derive\(.*Error.*\)\]'
# Test 2: Check for custom error implementations
rg -A 3 'impl.*Error.*for'
# Test 3: Look for error type definitions
ast-grep --pattern 'pub enum $name {
$$$
}'
Length of output: 124
Script:
#!/bin/bash
# Get the content of error.rs to analyze the error types and their implementations
cat src/error.rs
# Also check for any thiserror macro usage patterns
rg -p '#\[derive\(.*Error.*\)\]' src/error.rs -A 5
Length of output: 1985
src/utils/sqrt.rs (1)
14-14
: LGTM! Error message improvement is clear and descriptive.
The change from a generic Invalid error to Error::Invalid("NEGATIVE")
provides better context for debugging and aligns well with the PR's objective of improving error handling clarity.
src/error.rs (3)
1-2
: LGTM! Documentation and derive attributes are well structured.
The documentation comment clearly describes the purpose of these error types, and the derive attributes are properly consolidated.
5-9
: LGTM! Error messages are clear and informative.
The error messages for chain ID mismatch and equal addresses provide precise information about the error condition.
16-18
: LGTM! The rename improves domain clarity.
The rename from NotEqual
to CurrencyMismatch
better reflects the domain-specific nature of this error.
src/utils/sorted_insert.rs (1)
22-22
: LGTM! Core logic changes are consistent with new error handling.
The changes to return values align well with the simplified return type while preserving the original sorting behavior.
Also applies to: 33-33
src/entities/fractions/price.rs (2)
86-86
: LGTM! Improved error clarity in multiply method.
The change from Error::NotEqual
to Error::CurrencyMismatch
provides a more descriptive and specific error message when the quote currency doesn't match the other price's base currency.
105-105
: LGTM! Improved error clarity in quote method.
The change from Error::NotEqual
to Error::CurrencyMismatch
provides a more descriptive and specific error message when the input currency amount's currency doesn't match the base currency. This maintains consistency with the error handling in the multiply method.
src/entities/fractions/currency_amount.rs (3)
28-28
: LGTM! Improved error naming clarity.
The rename from MaxUint
to UintOverflow
better describes the error condition, making it more intuitive for developers to understand the issue.
91-91
: LGTM! Enhanced domain-specific error clarity.
The rename from NotEqual
to CurrencyMismatch
provides better context by explicitly indicating the nature of the mismatch, making it immediately clear that the error relates to incompatible currencies.
101-101
: LGTM! Consistent error handling with add operation.
The error type change maintains consistency with the add
method, ensuring symmetric error handling for both arithmetic operations.
src/entities/fractions/fraction.rs (1)
126-126
: LGTM! Consider making the error message even more descriptive.
The change from a generic Invalid
error to Invalid("SIGNIFICANT_DIGITS")
improves error clarity. However, consider making it even more descriptive by including the invalid value, e.g., "SIGNIFICANT_DIGITS_MUST_BE_POSITIVE".
Let's verify consistent error message usage across the codebase:
✅ Verification successful
Error messages follow a consistent pattern across the codebase
The verification shows that all Error::Invalid
instances use concise, uppercase static strings as error messages:
- "NEGATIVE" for negative number validation
- "DECIMALS" for decimal places validation
- "SIGNIFICANT_DIGITS" for significant digits validation
The current error message style aligns well with the codebase's conventions, so making it more verbose isn't necessary.
🏁 Scripts executed
The following scripts were executed for the analysis:
Script:
#!/bin/bash
# Description: Check for consistent error message patterns in Invalid errors
# Expected: All Invalid errors should use descriptive static strings
# Search for Error::Invalid usage
rg "Error::Invalid\(" -A 1
Length of output: 537
Upgraded
thiserror
version and made specific error messages clearer. Adjusted error variants and assertions to provide more meaningful feedback, enhancing clarity in debug and runtime scenarios.Summary by CodeRabbit
New Features
Bug Fixes
Refactor
sorted_insert
function, improving usability.Documentation
Error
enum to reflect new error types and their purposes.